package gui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.RowSorter;

import logic.Book;
import logic.CRUDBooks;
import logic.Teacher;

import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.Font;

import javax.swing.JSeparator;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;

import java.awt.event.WindowFocusListener;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.SwingConstants;

public class MainScreen extends JFrame {
	private JTable table;
	private JTextField textField;
	private String[] headerJTable = new String[] {"Portador","Sala", "Titulo do Livro", "Qualificacao","Status"};
	
	
	public static void main(String[] args) throws Exception {
		MainScreen s = new MainScreen(new Teacher("teste", "teste", "fdhfud", "123", "lad"));
		s.setVisible(true);
		s.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * Create the frame.
	 * @param teacher 
	 * @throws Exception 
	 */
	public MainScreen(final Teacher teacher) {
		
		
		setResizable(false);
		setTitle("Coolrent");
		setFont(new Font("Segoe UI", Font.PLAIN, 12));
		setBounds(100, 100, 800, 600);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(185, 95, 601, 466);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setShowGrid(true);
		
		updateTableView();
		
		scrollPane.setViewportView(table);
		
		
		final JLabel lblFulano = new JLabel("Bem vindo, " + teacher.getFirstName());
		lblFulano.setFont(new Font("Segoe UI", Font.PLAIN, 17));
		lblFulano.setBounds(10, 11, 305, 36);
		getContentPane().add(lblFulano);
		
		JButton btnAddLivro = new JButton("Adicionar Livro");
		btnAddLivro.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				final AddBook ab = new AddBook(teacher);
				ab.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				ab.setVisible(true);
			}
		});
		btnAddLivro.setBounds(10, 94, 163, 25);
		getContentPane().add(btnAddLivro);
		
		JButton btnEditarConta = new JButton("Editar conta");
		btnEditarConta.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				EditAccount ea = new EditAccount(teacher);
				ea.setVisible(true);
			}
		});
		btnEditarConta.setBounds(10, 58, 163, 25);
		getContentPane().add(btnEditarConta);
		
		textField = new JTextField();
		textField.setBounds(576, 57, 208, 25);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblPesquisar = new JLabel("Filtrar");
		lblPesquisar.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		lblPesquisar.setBounds(536, 63, 80, 20);
		getContentPane().add(lblPesquisar);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 45, 794, 2);
		getContentPane().add(separator);
		
		JButton btnMudarStatusDe = new JButton("Status de Livro");
		btnMudarStatusDe.setHorizontalAlignment(SwingConstants.RIGHT);
		btnMudarStatusDe.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				alertUnimplemented();
			}
		});
		btnMudarStatusDe.setBounds(10, 130, 163, 25);
		getContentPane().add(btnMudarStatusDe);
		
		JButton btnSair = new JButton("Sair");
		btnSair.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0); 
			}
		});
		btnSair.setBounds(10, 167, 163, 25);
		getContentPane().add(btnSair);
		
		JLabel lblLivros = new JLabel("Livros");
		lblLivros.setFont(new Font("Segoe UI", Font.PLAIN, 17));
		lblLivros.setBounds(194, 59, 305, 36);
		getContentPane().add(lblLivros);
		
		
		
		
		addWindowFocusListener(new WindowFocusListener() {
			public void windowGainedFocus(WindowEvent arg0) {
				lblFulano.setText("Bem vindo, " + teacher.getFirstName());
				updateTableView();
			}
			public void windowLostFocus(WindowEvent arg0) {
			}
		});
	}

	private void updateTableView() {
		try {
			table.setModel(new DefaultTableModel(fillTable(), headerJTable) {
				boolean[] columnEditables = new boolean[] { false, true, true };

				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	protected void alertUnimplemented() {
		JOptionPane.showMessageDialog(null, "Não implementado! :(" );
		
	}
	
    private Object[][] fillTable() throws FileNotFoundException, ClassNotFoundException, IOException {
		
		
		logic.CRUDBooks cb = new CRUDBooks();
		ArrayList<logic.Book> books = cb.getBooks();
		Object[][] tableContent = new Object[books.size()][headerJTable.length];
		
		
		for (int i = 0; i < books.size(); i++) {
			
			tableContent[i][0] = books.get(i).getHolder().getFirstName();
			tableContent[i][1] = books.get(i).getHolder().getSala();
			tableContent[i][2] = books.get(i).getTitle();
		    tableContent[i][3] = books.get(i).getRate();
			tableContent[i][4] = books.get(i).available() ? "Disponivel" : "Indisponível";
		}
			
		return tableContent;
		
		
	}
	
}
